package com.itmiaojie.utils;

import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtils {




    public static MimeMessage createSimpleMail(Session session, String mailfrom, String mailTo, String mailTittle,
                                               String mailText) throws Exception {
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 指明邮件的发件人
        message.setFrom(new InternetAddress(mailfrom));
        // 指明邮件的收件人，现在发件人和收件人是一样的，那就是自己给自己发
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        // 邮件的标题
        message.setSubject(mailTittle);
        // 邮件的文本内容
        message.setContent(mailText, "text/html;charset=UTF-8");
        // 返回创建好的邮件对象
        return message;
    }
    public static MimeMessage createSimpleMail2(MimeMessage session, String mailfrom, String mailTo, String mailTittle,
                                                String mailText) throws Exception {
        MimeMessage message = new MimeMessage(session);
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message,true);
        mimeMessageHelper.setFrom(mailfrom);
        mimeMessageHelper.setTo(mailTo);
        mimeMessageHelper.setText(mailText);
        mimeMessageHelper.setSubject(mailTittle);
        return message;
    }
    public static  MimeMessageHelper send
            (MimeMessage mimeMessage,String from,String to,String text,String subject) throws MessagingException {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setText(text);
        mimeMessageHelper.setSubject(subject);
    return mimeMessageHelper;
    }

}
